package com.amoyt.project.controller.admin;


import com.amoyt.project.annotations.BusinessLog;
import com.amoyt.project.annotations.ModuleLog;
import com.amoyt.project.domain.dto.insert.ArticleAddDto;
import com.amoyt.project.domain.dto.query.ArticlePageQuery;
import com.amoyt.project.domain.dto.update.ArticleUpdateDto;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.admin.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文章主表 前端控制器
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-25
 */
@RestController
@RequestMapping("/admin/article")
@ModuleLog(module = "公告新闻操作模块")
@Tag(name = "公告新闻管理接口",description = "文章管理接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 文章列表
     * @param query
     * @return
     */
    @Operation(summary = "文章列表",method = "POST")
    @PostMapping("/list")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result list(@RequestBody ArticlePageQuery query) {
        if (query == null || query.getPage() == null || query.getPageSize() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return articleService.list(query);
    }

    /**
     * 文章，公告详情（比如预览这种）
     * @param articleId
     * @return
     */
    @Operation(summary = "文章详情",method = "GET")
    @PostMapping("/detail/{articleId}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryArticleDetail(@PathVariable String articleId) {
        if (articleId == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return articleService.queryArticleDetail(articleId);
    }

    /**
     * 添加文章
     * @param articleAddDto
     * @return
     */
    @Operation(summary = "添加文章",method = "POST")
    @PostMapping("/add/{flag}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "添加文章")
    public Result add(@PathVariable Integer flag, @RequestBody ArticleAddDto articleAddDto) {
        if (articleAddDto == null || articleAddDto.getCategory() == null || articleAddDto.getIsOpen() == null || flag == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return articleService.add(flag,articleAddDto);
    }

    /**
     * 删除文章
     * @param articleIds
     * @return
     */
    @Operation(summary = "删除文章",method = "DELETE")
    @DeleteMapping("/delete/{articleIds}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin')")
    @BusinessLog(business = "删除文章")
    public Result delete(@PathVariable List<String> articleIds) {
        if (articleIds == null || articleIds.size() == 0){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return articleService.deleteBatch(articleIds);
    }

    /**
     * 修改文章
     * @param articleUpdateDto
     * @return
     */
    @Operation(summary = "修改文章",method = "PUT")
    @PutMapping("/update")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "修改文章")
    public Result update(@RequestBody ArticleUpdateDto articleUpdateDto) {
        if (articleUpdateDto == null || articleUpdateDto.getArticleId() == null || articleUpdateDto.getArticleId().equals("")){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return articleService.updateArticle(articleUpdateDto);
    }
}

